package com.work.crops.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.work.crops.pojo.CropsInfo;
import com.work.crops.pojo.CropsInfoIndex;
import com.work.crops.pojo.CropsParam;
import com.work.crops.service.CropsInfoService;
import com.work.entity.PageResult;
import com.work.entity.Result;
import com.work.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/crops")
public class CropsInfoController {

    @Autowired
    private CropsInfoService cropsInfoService;



    /**
     *
     * @param page  当前页数
     * @param size  每页数据大小
     * @param cropsParam 查询条件
     * @return
     */
    @GetMapping("/list/{page}/{size}")
    public Result findPageQuery(@PathVariable("page") Integer page,
                                    @PathVariable("size") Integer size,
                                    CropsParam cropsParam) {

//        分页条件查询
        PageInfo pageInfo = cropsInfoService.findByPage(page, size, cropsParam);

        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        return new Result(pageResult != null?true:false,
                pageResult != null?StatusCode.FIND_Ok:StatusCode.FIND_ERROR,
                pageResult != null ?"查询成功":"查询成功",
                pageResult);
    }

    /**
     *  根据id查询农作物
     * @param id    农作物id
     * @return
     */
    @GetMapping("/{id}")
    public Result<CropsInfo> findCropsById(@PathVariable("id") Integer id){
        CropsInfo cropsInfo = cropsInfoService.findById(id);
        return new Result(cropsInfo != null,
                cropsInfo != null?StatusCode.FIND_Ok:StatusCode.FIND_ERROR,
                cropsInfo != null?"查询成功":"查询失败",
                cropsInfo);
    }


    /**
     *  添加农作物信息
     * @param map 农作物的详情信息 , 以及数据指标id 、生长阶段
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Map<String,Object> map){

//        获取 cropsInfo 详情信息
        Object cropsInfoObj = map.get("cropsInfo");
//        将 Object 类型转换 CropsInfo 类型
        CropsInfo cropsInfo = JSON.parseObject(JSON.toJSONString(cropsInfoObj), CropsInfo.class);

//        获取 cropsInfoIndex 详情信息
        Object cropsInfoIndexObj = map.get("cropsInfoIndexs");
//        将 object 类型转换为 cropsInfoIndex 类型
        CropsInfoIndex[] cropsInfoIndexs = JSON.parseObject(JSON.toJSONString(cropsInfoIndexObj), CropsInfoIndex[].class);



        return cropsInfoService.addCropsInfo(cropsInfo,cropsInfoIndexs);
    }

    /**
     *  删除农作物
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable String id){
        return cropsInfoService.deleteById(id);
    }


    /**
     *  修改农作物
     * @param cropsInfo
     * @return
     */
    @PutMapping("/edit")
    public Result edit(@RequestBody CropsInfo cropsInfo){
       return cropsInfoService.updateCropsInfoById(cropsInfo);
    }
}
